package chao;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class Taxes extends JFrame {
	private static final long serialVersionUID = 531185107674986547L;

	// constants
	private final static String NEW_LINE = "\n";
	private final static String DIVIDER = ":   ";

	// default shown data records
	private static final int DEFAULT_DATA_RECORDS = 4;
	// we use variable containers for better expansibility
	private List<JTextField> names = new ArrayList<JTextField>(
			DEFAULT_DATA_RECORDS); // name
	private List<JTextField> prices = new ArrayList<JTextField>(
			DEFAULT_DATA_RECORDS); // price
	private List<JComboBox> types = new ArrayList<JComboBox>(
			DEFAULT_DATA_RECORDS); // category
	private List<JComboBox> ims = new ArrayList<JComboBox>(DEFAULT_DATA_RECORDS); // region

	private JTextField titleName, titlePrice, titleCategory, titleRegion;

	private String typeList[] = { "within tax", "book,food,medical" };
	private String imList[] = { "non-imported", "imported" };

	private JButton doCalculate;

	public Taxes() {
		super("SALES TAXES");
		Container container = getContentPane();
		container.setLayout(new FlowLayout());

		titleName = new JTextField("Product", 10);
		titleName.setEditable(false);
		container.add(titleName);
		titlePrice = new JTextField("Price", 5);
		titlePrice.setEditable(false);
		container.add(titlePrice);
		titleCategory = new JTextField("Category", 12);
		titleCategory.setEditable(false);
		container.add(titleCategory);
		titleRegion = new JTextField("Duty", 10);
		titleRegion.setEditable(false);
		container.add(titleRegion);

		for (int i = 0; i < DEFAULT_DATA_RECORDS; i++) {
			names.add(i, new JTextField(10));
			container.add(names.get(i));

			prices.add(i, new JTextField(5));
			container.add(prices.get(i));

			types.add(i, new JComboBox(typeList));
			types.get(i).setMaximumRowCount(2);
			container.add(new JScrollPane(types.get(i)));

			ims.add(i, new JComboBox(imList));
			ims.get(i).setMaximumRowCount(2);
			container.add(new JScrollPane(ims.get(i)));
		}

		setSize(500, 250);
		setVisible(true);

		doCalculate = new JButton("Receipt");
		doCalculate.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {
				StringBuilder msg = new StringBuilder("Print Receipt")
						.append(NEW_LINE); // prompt message

				DecimalFormat df = new DecimalFormat("0.00");

				double tax = 0.0d, total = 0.0d; // total tax & total payment
				double taxRate = 0.0d;

				for (int i = 0; i < DEFAULT_DATA_RECORDS; i++) {
					// if data is filled in, then calc it
					if (Util.isStringBlank(names.get(i))&&
							 Util.isStringBlank(prices.get(i)))
						continue;

					// shelf price
					double shelfPrice =0 ;
					try {
						 shelfPrice = Double
								.parseDouble(prices.get(i).getText());
					} catch (NumberFormatException e) {
						// if the input is not correct to the category, post a msg to user
						JOptionPane.showMessageDialog(null,
								"sorry, you got a wrong input, plz try again");
						return;
					}

					if (types.get(i).getSelectedIndex() == 0
							&& ims.get(i).getSelectedIndex() == 0) {
						taxRate = 10;
					} else if (types.get(i).getSelectedIndex() == 1
							&& ims.get(i).getSelectedIndex() == 0) {
						taxRate = 0;
					} else if (types.get(i).getSelectedIndex() == 0
							&& ims.get(i).getSelectedIndex() == 1) {
						taxRate = 15;
					} else {
						taxRate = 5;
					}

					double unitTax = Math
							.ceil(((taxRate * shelfPrice / 100) * 20)) / 20; // tax for every product
					double payment = shelfPrice + unitTax; // every product you have to pay
					tax += unitTax;
					total += payment;
					msg.append(names.get(i).getText()).append(DIVIDER).append(
							df.format(payment)).append(NEW_LINE);
				}

				msg.append("Sales_Taxes").append(DIVIDER)
						.append(df.format(tax)).append(NEW_LINE);
				msg.append("Total").append(DIVIDER).append(df.format(total));

				JOptionPane.showMessageDialog(null, msg.toString());
			}
		});
		container.add(doCalculate);
	}

	public static void main(String args[]) {
		Taxes application = new Taxes();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class Util {
	public static boolean isStringBlank(JTextField input) {
		if (null == input || null == input.getText()
				|| "".equals(input.getText().trim()))
			return true;
		else
			return false;
	}
}
